home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------------------------
- // Application Document Functions
- //
- // by Philip McBride
- //
- //--------------------------------------------------------------------------------------------
-
-
- #include "GameControls.h"
- #include "extern.h"
- #include "document.h"
- #include "context.h"
- #include "camera.h"
- #include "lights.h"
-
- //--------------------------------------------------------------------------------------------
- // Create a new Document
- //
- DocumentPtr MyNewDocument()
- {
- DocumentPtr theDocument;
- CWindowPtr theWindow;
- TQ3Status myStatus;
- TQ3RendererObject myRenderer;
- Rect theRect = { 0, 0, 16, 16 };
- TQ3Param2D uvValues = {-1.0, -1.0};
- TQ3DrawContextObject theDrawContext ;
- Rect myBounds = kMyBoundsRect;
- TQ3CameraObject camera = NULL ;
-
- // create the document record
- theDocument = (DocumentPtr )NewPtrClear(sizeof(DocumentRecord));
-
- // create the window to display the model and add referneces to the document
- theWindow = (CWindowPtr)NewCWindow(0L,&myBounds,"\pModel Window",true,documentProc,(WindowPtr)-1L,true,NULL);
- if (theWindow == nil)
- goto bail;
- theDocument->theWindow = theWindow;
- SetWRefCon((WindowPtr)theWindow, (long)theDocument ) ;
-
- // add the window dimensions (width and height) to the document
- theDocument->width = myBounds.right - myBounds.left;
- theDocument->height = myBounds.bottom - myBounds.top;
-
- // zero out the file spec
- theDocument->theFileSpec.vRefNum = 0 ;
- theDocument->theFileSpec.parID = 0 ;
- theDocument->theFileSpec.name[0] = '\0' ;
-
- // Create the new draw context
- theDrawContext = MyNewDrawContext( (WindowPtr)theWindow ) ;
- if (theDrawContext == NULL)
- goto bail;
-
- // Create the view and assign the draw context
- theDocument->theView = Q3View_New();
- if (theDocument->theView == NULL)
- goto bail;
- if ((myStatus = Q3View_SetDrawContext(theDocument->theView, theDrawContext)) == kQ3Failure )
- goto bail;
- Q3Object_Dispose(theDrawContext);
-
- // Add more model and view properties to the document record
- theDocument->documentGroup = nil;
- theDocument->currentInterpolation = kQ3InterpolationStylePixel;
- theDocument->illuminationShader = Q3PhongIllumination_New();
-
- // Use the interactive software renderer
- if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
- if ((myStatus = Q3View_SetRenderer(theDocument->theView, myRenderer)) == kQ3Failure ) {
- goto bail;
- }
- // these two lines set us up to use the best possible renderer,
- // including hardware if it is installed.
- Q3InteractiveRenderer_SetDoubleBufferBypass (myRenderer, kQ3True);
- Q3InteractiveRenderer_SetPreferences(myRenderer, kQAVendor_BestChoice, 0);
-
- }
- else {
- goto bail;
- }
- Q3Object_Dispose( myRenderer ) ;
-
- // Setup the window gworld
- SetGWorld(theWindow,nil);
-
- return(theDocument);
-
- bail:
- if (theWindow)
- DisposeWindow((WindowPtr) theWindow);
-
- if (theDocument)
- DisposePtr((Ptr)theDocument);
-
- return( (DocumentPtr )nil );
- }
-
-
- //--------------------------------------------------------------------------------------------
- // Delete a Document
- //
- void MyCloseDocument( DocumentPtr theDocument )
- {
- // if it has a file associated, close it
- if (theDocument->fRefNum)
- FSClose(theDocument->fRefNum);
-
- // and we can dispose of the window
- if(theDocument->theWindow)
- DisposeWindow((WindowPtr)theDocument->theWindow);
-
- // dispose of our QuickDraw 3d view object
- if(theDocument->theView)
- Q3Object_Dispose(theDocument->theView);
-
- // if we have a group associated with the document, then delete that
- if(theDocument->documentGroup)
- Q3Object_Dispose(theDocument->documentGroup);
-
- if(theDocument->illuminationShader)
- Q3Object_Dispose(theDocument->illuminationShader);
-
- // finally dispose of the storage used for the document and
- // decrement the document count
- if( theDocument != nil )
- DisposPtr((Ptr) theDocument);
- }
-
- //--------------------------------------------------------------------------------------------
- // Find a Document from a Window
- //
- DocumentPtr MyGetDocumentFromWindow(theWindow)
- {
- return (DocumentPtr)GetWRefCon((WindowPtr)theWindow);
- }
-